package com.ytd.smartcanteen.weighting.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.ytd.smartcanteen.weighting.R;

/**
 * Your class notes
 *
 * @author gaoguanchao
 * @since 2023/5/18
 */
public class ChooseListAdapter extends BaseAdapter {
    private final ArrayList<String> mStrings = new ArrayList<>();
    private final LayoutInflater mInflation;

    private int textColor = -1;

    private int currentItem;

    public ChooseListAdapter(Activity activity, ArrayList<String> data) {
        super();
        mStrings.addAll(data);
        mInflation = activity.getLayoutInflater();
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    @Override
    public String getItem(int position) {
        return mStrings.get(position);
    }

    public void clear() {
        mStrings.clear();
    }

    @Override
    public int getCount() {
        return mStrings.size();
    }

    public void setCurrentItem(int i) {
        currentItem = i;
        notifyDataSetChanged();
    }

    public String getCurrentItem() {
        return mStrings.get(currentItem);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        // 一般列表视图的优化代码。
        if (view == null) {
            view = mInflation.inflate(R.layout.list_choose2, null);
            viewHolder = new ViewHolder();
            viewHolder.chooseName = view.findViewById(R.id.choose_name);
            viewHolder.chooseImg = view.findViewById(R.id.choose_img);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.chooseName.setText(mStrings.get(i));
        if (textColor != -1) {
            viewHolder.chooseName.setTextColor(textColor);
            viewHolder.chooseImg.setVisibility(View.GONE);
            if (i == currentItem) {
                viewHolder.chooseName.setTextColor(
                    view.getContext().getResources().getColor(android.R.color.holo_red_dark));
            }
        } else {
            if (i == currentItem) {
                viewHolder.chooseImg.setVisibility(View.VISIBLE);
            } else {
                viewHolder.chooseImg.setVisibility(View.GONE);
            }
        }
        return view;
    }

    static class ViewHolder {
        TextView chooseName;
        ImageView chooseImg;
    }
}



